// pages/baby/add/add.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
const date = require('../../utils/date.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    measure_time:'2010-01-01',
    before_measure_time:'2010-01-01',
    log:{},
  },

  loadData:function(e){
    var that = this;
    util.request('baby/currentChild', {}, function (data) {
      if(data.code == 1){
        that.setData({
          child:data.data.data,
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  /**
   * 获取三个月之前的时间
   */
  beforeMonth: function () {
    var month = new Date().getMonth();
    var time = date.formatDate(new Date(new Date().setMonth(month-3))) ;
    this.setData({
      before_measure_time: time
    })
  },

  measureTimeChange(e) {
    this.setData({
      measure_time: e.detail.value
    })
  },
  measureChange(e) {
    this.setData({
      before_measure_time: e.detail.value
    })
  },

  submit: function (e) {
    var data = e.detail.value;
    console.log(e.detail.value);
    var that = this
    data.measure_time = that.data.measure_time;
    data.baby_id = that.data.child.id;
    data.before_measure_time = that.data.before_measure_time;
    if (data.height == '') {
      util.fail("请输入宝贝身高", that)
      return
    }
    if (data.weight == '') {
      util.fail("请输入宝贝体重", that)
      return
    }

    util.request('baby_grow_log/add',data,
        function (data) {
          if(data.code == 1){
            util.success();
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/grow_log/grow_log',
              })
            },1500)

          }else{
            util.fail(data.msg,that);
          }
        });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.beforeMonth();
      this.setData({
        measure_time:date.formatDate(new Date(new Date()))
      })
      this.loadData();
      

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})